var app = getApp();

import api from '../../../utils/api.js'
var app = getApp();
var QQMapWX = require("../../../utils/qqmap-wx-jssdk.js");
var qqmapsdk;
import { HTTP } from '../../../utils/http.js'
import utils from '../../../utils/util.js'
import { config } from '../../../utils/config'

let http = new HTTP()

Page({

  data: {
    imgUrl: config.imgUrl,
    isFind : false,
    searchObj:{
      searchBg : "#fff",
      placeStr:'请输入菜品名称'
    }

  },
  onLoad: function (options) {
    let kw = options.kw;
    let id = options.id
    let that = this;
    
    if(kw){
      this.setData({
        inputValue : kw,
        id: id,
        deleteBtn : true
      })
      this.searchProduct();
    }
  },
  searchProduct(){
    let address = wx.getStorageSync('address');
    let data = {
      lat: address.latitude,
      lng: address.longitude,
      keyword : this.data.inputValue,
      id : this.data.id ? this.data.id : ''
    }
    this.getInfo(api.searchProduct,"GET",data).then((res) => {
      this.setData({
        cookbook : res.cookbook,
        product : res.product,
        isFind : true
      })
      
    })

  },
  addBasket(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url : '/pages/classify/addBasket/addBasket?id='+id
    })
  },

  showVideo(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "/pages/video/video?id=" + id
   })
  },

  /*获取菜品一级分类*/ 
  getInfo(url, method ,data,loadding=true) {
    return new Promise((resolve, reject) => {
        if(loadding){
          wx.showLoading({
            title: '加载中...',
          }) 
        }    
        http.request({
            url: url,
            method: method,
            data: data,
            success: res => {
              if(res.msg && res.error_code){
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 1500
                });
                return ;
              }
              if(res.code == 200){
                resolve(res.data);
              }
              if(loadding){
                wx.hideLoading()
              } 
              
            },
            fail: () => {},
            complete: () => {
              wx.hideLoading()
            }

            
        })
    })
  },

  
   /*搜索按钮*/
 searchHandle(e){
  let inputValue = e.detail.inputValue
  this.setData({
    inputValue
  })
  this.setData({
    page : 1,
    cookList : []
  })
  this.searchProduct();
  

},
  
 
 
})